package com.kuang.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.kuang.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117696810";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDrp4ic5ZH6cpQZFDgOqoT9QRr31yMA29dPUSRpHauuuunfXru+JVZjKuhvPetJWlJEyOpqXE2TZcS3dBerybqu0sMeNZmTS3WZT6ULh2RVPaZGZTEre0TgkuLbFgLRwAlB8VtgMB5WI2jJZXm7XuxwV8oWqCSbU/pNGieMZV6Ch67rC9yuDV/+gbrhOOGlhnjSgVnYBcM/O4lRhPIPBdjRwNxlgX2p+9U1SANYskSirr+698dKRD28mGku8ZotdyWHBM8gv+L+aoihuxDab1FzZbPp+QFk0UkPWvcM88o6NIxHTkoQX3XOCmAN8dATe8qblePa+nPCTXPq1ga+JmrAgMBAAECggEAGlDGJ2pkCQEOvlwd0YHHxW88r/YWP6DcG40H6/crBKY0O+eWrrUc26WCEzN5Z6/HR6Q8h0Zmaoy8+Tc80f88zsIZMGfhqNYZp/PZurEwlX+ePGBhW6DKdRCf2kAPpElPHRddfBGz5UwuGgGD4H9rLc3QH3dlCX4w7xDbK2oLTjT4A5+ZAmmceXmqLVHAcK+WX6MEImi97foE61aYHkkqXrxScImyI9pic2OqLE0oOCWaZhOnq5fsvKvpnQGolwpgzxWB+fIacCPRiyBcv5J8yCv4nkpxCf6viHb6R9NxEYzS9Y8y7y0t8m96IMj6u9Y5vU1/+srq1mRRulmLDJb9cQKBgQDDM4CCWA+mZy1p0GbC9YmfGDYY+24i1zATJfCNHaFzsbvn0YRYQEdK0w8kqLc1GnJChPOyhZS8pXrbWYLok8gowCpyeEq1B9RBSmmb+StZomJ3v8S4TaEwyU2Ca452+Chzp261wiVIpYpmipWZuws0q+G4jH9tu066vVVFDa3WwwKBgQCssmEGkW4eBNbw5uQUNGXiTpQsHYHyM86241piWfgoC2eMqkoJjO3DGf+7FgdCoV8U0Lcv6P+TB/qi/Vf+DNoAqfBHMhvI6xyjrhxqvNcKR1ICG88/GZ/qWNDvitfzzMr+G43IaPs2AMPljTbWitoLwhjhircNSBwh7TtTuoMS+QKBgFyaq0Wq50nHOwkvAAFPoQlj4A48AcTvl3QOFK+ttfGACSfyAronTGsX8+AtVjUQJdKe5kn0C1rvmnhDX9JcgaGrgLerj62QWEbdOXrwW3t7iQFMbdQNqAHQDkBoLD8OfYpYjTIXN77eIemBp3U2AhvryvrVVILHUJNQzQokcisnAoGAfL5AvYjRHNfsFokBWOLccVLHTvJlifkIKS0ifYZ1bR+HxaC/6BQnLlWaVp470lJUJun4WKAZ9C/ASHltVPIdins2N4cncML7pfO/TQ5o2mDYCPPjiLXOwG01RsJo6Q8V1eU7Tfce9kwpNKgDY4dap0GlzUzwmqRqxcrhnsJLqakCgYEAsr3KE9ab8K4V6/mxywVldQ+p1nlSKfKve4/mi1omKuISFvv29nlZPMasPR/kl6+s6sQva6G9fj7YICTy1rfefpn0d1ZeyrbaR59ldGA64+Wgq2cdYypb1O/zE0lZBUcYxTUqJcmMO6Aiw06FXEGrGXOnfPGIjh3Q8c/EMG7GsOQ=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApUlozyk7NgCpbdSFuSWNXs37tG5pWYiLQpPeARRd9NlTj24kb7QtXSqa0Yiigtn2zZpJ05CSXP8HgMlt3voOmaeEQ8Ssq4i6tUNAi2b2rEkLZPItAfttWPMqKIXZarQqYqGfrDEeMl+ayE9tfKOtKeHaOhKM7i22dtQ41BuxOvi+9JDyVteq8zaOApb3MQcDACSviAcia0JJd/UjdFpIoBRkdkFkf8oKkJcpcZhRy0W31NlV4zzgdXzhS+soZaJHUPKPa4x00UTWyBzG1t8WDx+6aTggosgekFjgic47WLS5vM5IqasUtCrVd/cRYm52idWYvEYxWNZi84lnB6WXJQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://order.xiangss.top/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //超时时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
